package org.kgc.alanhealth.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
@Component
public class SmsUtils {
    private static final String ACCESS_KEY_ID = "LTAI5tJ148whEYaYKkZDo1Q1";
    private static final String ACCESS_KEY_SECRET = "D4ILAY5ryoQcZclRDAi9UmwIzuRFVC";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";

    public static void sendSms(String signName, String templateCode, String phoneNumber, String templateParam) throws Exception {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ACCESS_KEY_ID)
                .accessKeySecret(ACCESS_KEY_SECRET)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region(REGION_ID)
                .credentialsProvider(provider)
                .overrideConfiguration(ClientOverrideConfiguration.create()
                        .setEndpointOverride(ENDPOINT))
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName(signName)
                .templateCode(templateCode)
                .phoneNumbers(phoneNumber)
                .templateParam(templateParam)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }
}